package com.sllp.C71S3TsllpShop.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.github.pagehelper.PageHelper;
import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.Car;
import com.sllp.C71S3TsllpShop.bean.Orders;
import com.sllp.C71S3TsllpShop.bean.OrdersItem;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.impl.CarBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.OrdersBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.OrdersItemBizImpl;
import com.sllp.C71S3TsllpShop.dao.OrdersItemMapper;
import com.sllp.C71S3TsllpShop.dao.OrdersMapper;

@Controller
public class OrderAction {

	@Resource
	private OrdersBizImpl obiz;
	
	@Resource 
	private OrdersItemMapper oim;
	
	@Resource
	private OrdersMapper om;
	
	@Resource
	private OrdersItemBizImpl oibiz;
	
	@Resource
	private CarBizImpl cbiz;
	
	//显示我的订单页面
	@GetMapping("MemberOrder.do")
	public String order( HttpSession session,@SessionAttribute("btlist") List<List<BigType>> lists,
			Model m,String pre,String next,
			@SessionAttribute("loginUser") User user,
			@SessionAttribute(name="page",required=false)Integer page) {
		m.addAttribute("btlist", lists);
		if(next==null){
			page=1;
		}else if(Integer.valueOf(next)>0){
			page=Integer.valueOf(next);
		}
		if(pre!=null){
			page=1;
		}
		PageHelper.startPage(page,5);
		//从新设置分页的页码，相当于page++
		System.out.println(page+"========");
	    m.addAttribute("page", page);
	    m.addAttribute("pag", page-1);
	    
		//显示所有的订单
		List<Orders> orders =obiz.findOrder(user);
		m.addAttribute("order", orders);
		
		//右上角购物车显示
//		List<Car> clist=cbiz.findCar();
//		m.addAttribute("clist", clist);
		return "Member_Order";
	}
	
	//删除订单
	@ResponseBody
	@PostMapping("delOrder")
	public Result deleteOrders(String id){
		OrdersItem ordersItem = new OrdersItem();
		ordersItem.setId(id);
		ordersItem.setState(0);
		int ok=oibiz.updateOrdersItemState(ordersItem);
		if(ok>0) {
			return new Result(ok,"删除成功");
		}
		return new Result(ok, "删除失败"); 
	}
	
	// 取消订单
	@ResponseBody
	@PostMapping("cancelOrder")
	public Result cancelOrder(String id){
		OrdersItem ordersItem = oibiz.findpro(id);
		if(ordersItem.getState() == 1) {
			ordersItem.setState(9);
		}else if(ordersItem.getState() == 2) {
			ordersItem.setState(5);
		}
		
		int ok=oibiz.updateOrdersItemState(ordersItem);
		if(ok>0) {
			return new Result(ok,"取消成功");
		}
		return new Result(ok, "取消失败"); 
	}
	
	//确认收货
	@ResponseBody
	@PostMapping("receipt")
	public Result receipt(String id){
		int ok=oibiz.confirmReceipt(id);
		if(ok>0) {
			return new Result(ok,"确认收货成功");
		}
		return new Result(ok, "确认收货失败"); 
	}
	
	@ModelAttribute
	public void searchCar(@SessionAttribute("loginUser") User user,Model m) {
		if(user != null) {
			List<Car> clist=cbiz.findCar(user);
			m.addAttribute("clist", clist);
		}
	}
}
